require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32ole'
require 'win32/process'

class TC_ManageUser_test < Test::Unit::TestCase
   
    def startup
      $UserName = 'NewUser'
      $UserPassword= '123456'
      $UserEmail = 'newuser@user.com'
      $UserNameEdit = 'NewUserEdit'
      $PasswordEdit = '123456789'
      $TotalUser ='30'
      $MemberName = 'Sajid'
      $Location = 'India'
      $Occupation = 'Professional'
      $Interests = 'sports,blogging'
      $WebAddress = 'http://www.greatdeccan.com'
      $WebLog ='http://greatdeccan.blogspot.com'
      $WebGallery ='http://greatdeccan.yahoophotos.com'
      $AvatarUrl ='http://greatdeccan.com'
      $Bio = 'Hi \nThis is the first user'
      $Signature = 'Regards \nSajid'
      $PublicEmail ='sajid@greatdeccan.com'
      $MsnIM ='sajid@live.com'
      $YahooIM = 'sajid.greatdeccan@yahoo.com'
      $AolIM='sajid.greatdeccan@aol.com'
      $Icq ='sajid.greatdeccan@icq.com'
      $FilePath ='../resources/Images/gallery_slideshow.gif'
      $Role ='Role1'
      $RoleDescription = 'This is a new role'
      @@ie = Watir::IE.start($SITE_URL)
      
     
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
     def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id
        
    end  
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        ie.button(:id, /loginButton$/).click
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToMemberAdministration
        ie.goto($SITE_URL + 'controlpanel/membership/')
        assert(ie.contains_text('Membership Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Membership Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      
    def test_10_navigateToCreateUserAccount
        ie.goto($SITE_URL +'controlpanel/membership/UserAdd.aspx')
        assert(ie.text_field(:id,/username$/).exists?)
        ie.text_field(:id,/username$/).set($UserName)
        assert(ie.text_field(:id,/password$/).exists?)
        ie.text_field(:id,/password$/).set($UserPassword)
        password=ie.text_field(:id,/password$/).value
        assert(ie.text_field(:id,/password2$/).exists?)
        ie.text_field(:id,/password2$/).value =password
        assert(ie.text_field(:id,/email$/).exists?)
        ie.text_field(:id,/email$/).set($UserEmail)
        assert(ie.select_list(:id,/timezone$/).exists?)
        ie.select_list(:id,/timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
        assert(ie.button(:name,/createButton$/).exists?)
        ie.button(:name,/createButton$/).click
      end
    def test_11_CreateNewRole
        ie.goto($SITE_URL+'controlpanel/membership/Roles.aspx')
        assert(ie.button(:id,/AddRole$/).exists?)
        ie.button(:id,/AddRole$/).click
        sleep 5
        assert(ie.frame(:index, 1).text_field(:id, /Name$/).exists?)
        ie.frame(:index, 1).text_field(:id, /Name$/).value = $Role
        sleep 2
        assert(ie.frame(:index, 1).text_field(:id, /Description$/).exists?)
        sleep 2
        ie.frame(:index, 1).text_field(:id, /Description$/).value = $RoleDescription
        assert(ie.frame(:index, 1).link(:id, /SaveButton$/).exists?)
        sleep 3 
        ie.frame(:index, 1).link(:id, /SaveButton$/).click
 
    end
    
    def test_12_navigateToManageUsers 
        ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
        assert(ie.text_field(:id,/searchText$/).exists?)
        ie.text_field(:id,/searchText$/).value ="*"
        assert(ie.select_list(:name,/PageList$/).exists?)
        ie.select_list(:name,/PageList$/).select($TotalUser)
        assert(ie.button(:id,/searchButton$/).exists?)
        ie.button(:id,/searchButton$/).click
      end
      def test_13_navigateToEditUser
        table1 = ie.table(:index, 6)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$UserName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Edit').click
               break
             end   
         rowcount1=rowcount1+1
        end
       if(ok ==0)
         sleep 5
         ie.link(:text,'Next >').click
         sleep 5
         test_13_navigateToEditUser
       end
    end
    def test_14_editUser
      
       assert(ie.link(:id,/changeUserName$/).exists?)
       ie.link(:id,/changeUserName$/).click
       sleep 5
       assert(ie.frame(:index,3).text_field(:id,/DesiredUserName$/).exists?)
       ie.frame(:index,3).text_field(:id,/DesiredUserName$/).value = $UserNameEdit
       assert(ie.frame(:index,3).checkbox(:id,/IgnoreDisallowedNames$/).exists?)
       ie.frame(:index,3).checkbox(:id,/IgnoreDisallowedNames$/).set
       assert(ie.frame(:index,3).button(:id,/ChangeUserNameButton$/).exists?)
       ie.frame(:index,3).button(:id,/ChangeUserNameButton$/).click
       sleep 2
       assert(ie.link(:id,/changePassword$/).exists?)
       ie.link(:id,/changePassword$/).click
       sleep 5
       assert(ie.frame(:index,3).text_field(:id,/NewPassword1$/).exists?)
       ie.frame(:index,3).text_field(:id,/NewPassword1$/).value = $PasswordEdit
       assert(ie.frame(:index,3).text_field(:id,/NewPassword2$/).exists?)
       ie.frame(:index,3).text_field(:id,/NewPassword2$/).value = $PasswordEdit
       assert(ie.frame(:index,3).button(:id,/ChangePasswordButton$/).exists?)
       ie.frame(:index,3).button(:id,/ChangePasswordButton$/).click
       sleep 2
       assert(ie.link(:id,/changeRoles$/).exists?)
       ie.link(:id,/changeRoles$/).click
       sleep 5
       assert(ie.frame(:index, 3).select_list(:id, /listAvailableRoles$/).exists?)
       ie.frame(:index,3).select_list(:id, /listAvailableRoles$/).set($Role)
       assert(ie.frame(:index,3).button(:value, />/).exists?)
       sleep 5
       ie.frame(:index,3).button(:value, />/).click
       assert(ie.frame(:index,3).link(:id, /btnSave$/).exists?)
       sleep 5
       ie.frame(:index,3).link(:id, /btnSave$/).click
       sleep 5
       assert(ie.select_list(:id,/accountStatus$/).exists?)
       ie.select_list(:id,/accountStatus$/).select('Approved')
       assert(ie.select_list(:id,/moderationLevel$/).exists?)
       ie.select_list(:id,/moderationLevel$/).select('User is Moderated')
       assert(ie.text_field(:id,/dummyTotalPosts$/).exists?)
       ie.text_field(:id,/dummyTotalPosts$/).value ='0'
       # About User
       ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.GetTabAtIndex(1))")
       assert(ie.text_field(:id,/CommonName$/).exists?)
       ie.text_field(:id,/CommonName$/).set($MemberName)
       assert(ie.text_field(:id,/location$/).exists?)
       ie.text_field(:id,/location$/).set($Location)
       assert(ie.text_field(:id,/occupation$/).exists?)
       ie.text_field(:id,/occupation$/).set($Occupation)
       assert(ie.text_field(:id,/interests$/).exists?)
       ie.text_field(:id,/interests$/).set($Interests)
       assert(ie.radio(:id,/enableBirthdayRadioButton$/).exists?)
       ie.radio(:id,/enableBirthdayRadioButton$/).set
       assert(ie.text_field(:id,/birthdayDateTimeSelector_text$/).exists?)
       ie.text_field(:id,/birthdayDateTimeSelector_text$/).value ='April 15, 1978'
       assert(ie.radio(:id,/gender_1$/).exists?)
       ie.radio(:id,/gender_1$/).set
       assert(ie.text_field(:id,/webAddress$/).exists?)
       ie.text_field(:id,/webAddress$/).set($WebAddress)
       assert(ie.text_field(:id,/webLog$/).exists?)
       ie.text_field(:id,/webLog$/).set($WebLog)
       assert(ie.text_field(:id,/webGallery$/).exists?)
       ie.text_field(:id,/webGallery$/).set($WebGallery)
       assert(ie.radio(:id,/sharePosts_0$/).exists?)
       ie.radio(:id,/sharePosts_0$/).set
       assert(ie.radio(:id,/shareSections_0$/).exists?)
       ie.radio(:id,/shareSections_0$/).set
       sleep 4
       ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_bio_ctl00_Editor').execCommand('mceSetContent', false,'"+$Bio+"')")
       sleep 4
       # Avatar
       ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.GetTabAtIndex(2))")
       assert(ie.radio(:id,/isAvatarApproved_0$/).exists?)
       ie.radio(:id,/isAvatarApproved_0$/).set
       assert(ie.radio(:id,/enableAvatar_0$/).exists?)
       ie.radio(:id,/enableAvatar_0$/).set
       assert(ie.fileField(:id,/uploadedAvatar$/).exists?)
       ie.fileField(:id,/uploadedAvatar$/).set($FilePath)
       assert(ie.button(:id,/avatarUpdateButton$/).exists?)
       ie.button(:id,/avatarUpdateButton$/).click
       #~ assert(ie.text_field(:id,/avatarUrl$/).exists?)
       #~ ie.text_field(:id,/avatarUrl$/).value =$AvatarUrl
       # Site Options
       ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.GetTabAtIndex(3))")
       assert(ie.select_list(:id,/language$/).exists?)
       ie.select_list(:id,/language$/).select('U.S. English')
       assert(ie.select_list(:id,/timezone$/).exists?)
       ie.select_list(:id,/timezone$/).select('(GMT) Greenwich Mean Time : Dublin, London')
       assert(ie.select_list(:id,/dateFormat$/).exists?)
       ie.select_list(:id,/dateFormat$/).select('Month Days, Year (Jun 1, 2003)')
       assert(ie.select_list(:id,/sortOrder$/).exists?)
       ie.select_list(:id,/sortOrder$/).select('[A-Z]')
       assert(ie.select_list(:id,/FontSize$/).exists?)
       ie.select_list(:id,/FontSize$/).select('Normal')
       assert(ie.select_list(:id,/EditorList$/).exists?)
       ie.select_list(:id,/EditorList$/).select('Plain Text')
       assert(ie.select_list(:id,/forumDefaultView$/).exists?)
       ie.select_list(:id,/forumDefaultView$/).select('Threaded')
       assert(ie.radio(:id,/enableDisplayNames_0$/).exists?)
       ie.radio(:id,/enableDisplayNames_0$/).set
       assert(ie.radio(:id,/enableDisplayNames_0$/).exists?)
       ie.radio(:id,/enableDisplayNames_0$/).set
       assert(ie.radio(:id,/displayInMemberList_0$/).exists?)
       ie.radio(:id,/displayInMemberList_0$/).set
       assert(ie.radio(:id,/EnableCollapsingPanels_0$/).exists?)
       ie.radio(:id,/EnableCollapsingPanels_0$/).set
       assert(ie.radio(:id,/conversationNotifications_0$/).exists?)
       ie.radio(:id,/conversationNotifications_0$/).set
       assert(ie.radio(:id,/commentNotifications_0$/).exists?)
       ie.radio(:id,/commentNotifications_0$/).set
       assert(ie.radio(:id,/enableEmoticons_0$/).exists?)
       ie.radio(:id,/enableEmoticons_0$/).set
       assert(ie.radio(:id,/enableUserAvtars_0$/).exists?)
       ie.radio(:id,/enableUserAvtars_0$/).set
       assert(ie.radio(:id,/enableUserSignatures_0$/).exists?)
       ie.radio(:id,/enableUserSignatures_0$/).set
       sleep 4
       ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_signature_ctl00_Editor').execCommand('mceSetContent', false,'"+$Signature+"')")
       sleep 4 
       # Email and Contact Info
       ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_UserEditTabs.GetTabAtIndex(4))")
       assert(ie.radio(:id,/enableEmail_0$/).exists?)
       ie.radio(:id,/enableEmail_0$/).set
       assert(ie.radio(:id,/enableHtmlEmail_0$/).exists?)
       ie.radio(:id,/enableHtmlEmail_0$/).set
       assert(ie.radio(:id,/enableEmailTracking_0$/).exists?)
       ie.radio(:id,/enableEmailTracking_0$/).set
       assert(ie.text_field(:id,/publicEmail$/).exists?)
       ie.text_field(:id,/publicEmail$/).set($PublicEmail)
       assert(ie.text_field(:id,/msnIM$/).exists?)
       ie.text_field(:id,/msnIM$/).set($MsnIM)
       assert(ie.text_field(:id,/aolIM$/).exists?)
       ie.text_field(:id,/aolIM$/).set($AolIM)
       assert(ie.text_field(:id,/yahooIM$/).exists?)
       ie.text_field(:id,/yahooIM$/).set($YahooIM)
       assert(ie.text_field(:id,/icq$/).exists?)
       ie.text_field(:id,/icq$/).set($Icq)
       assert(ie.link(:id,/updateButton$/).exists?)
       ie.link(:id,/updateButton$/).click
   
    end
    def test_15_navigateToDeleteUser
        ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
        assert(ie.text_field(:id,/searchText$/).exists?)
        ie.text_field(:id,/searchText$/).value ="*"
        assert(ie.button(:id,/searchButton$/).exists?)
        ie.button(:id,/searchButton$/).click
    end
    def test_16_navigateToUser
        table1 = ie.table(:index, 6)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$UserNameEdit).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Delete').click
               break
             end   
         rowcount1=rowcount1+1
        end
       if(ok ==0)
         sleep 2
         ie.link(:text,'Next >').click
         sleep 2
         test_16_navigateToUser
       end
    end
    def test_17_assignToAnonymous     
       sleep 5
       assert(ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).exists?)
       ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).set
       assert(ie.frame(:index,1).button(:id,/DeleteUserButton$/).exists?)
       ie.frame(:index,1).button(:id,/DeleteUserButton$/).click
       sleep 5
     end
     
     def test_18_navigateToRoles
        ie.goto($SITE_URL +'controlpanel/membership/Roles.aspx')
      end
      
      def test_19_navigateToDeleteRole
        table1 = ie.table(:index, 5)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$Role).exists?)
              ok =1
               sleep 5
               table1[rowcount1][3].button(:value, 'Delete').click
               break
             end   
        rowcount1=rowcount1+1
        end
        if(ok ==0)
        sleep 5
        ie.link(:text,'Next >').click
        sleep 5
        test_19_navigateToDeleteRole
        sleep 5
      end
    end
         
    def test_99_closeIE
      ie.close
    end      

end
